package cn.mogugo.joke.tld;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.hibernate.util.StringHelper;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.mogugo.joke.manager.JokeCategoryManager;
import cn.mogugo.joke.model.JokeCategory;

public class JokeCategoryTld extends BodyTagSupport {
	/**
	 * 数量
	 */
	private Integer pageSize;
	
	
	public int doEndTag() throws JspException {
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletConfig().getServletContext());
		JokeCategoryManager jokeCategoryManager = (JokeCategoryManager) context.getBean("jokeCategoryManager");
		
		List listCategory = jokeCategoryManager.getListOrderTld(pageSize);
		for(int i = 0 ; i < listCategory.size() ; i ++ ){
			BodyContent bc = getBodyContent();
			String body = bc.getString();
			
			JokeCategory jc = (JokeCategory)listCategory.get(i);
			body = StringHelper.replace(body, "$_jc_category_name" , jc.getCategory_name());
			body = StringHelper.replace(body, "$_jc_joke_number" , jc.getJoke_number()+"");
			body = StringHelper.replace(body, "$_jc_category_id" , jc.getId()+"");

	        try {
	            pageContext.getOut().print(body);
	        } catch (IOException e) {
	        }
		}

		
		return this.EVAL_PAGE;
				
		
	}


	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	
	
	
}
